<template>
    <div class="resource-list">
      <el-card class="box-card">
        <div slot="header" class="clearfic">
          <!-- 头部结构 -->
          <el-form :inline="true" :model="form" class="demo-form-inline" ref="form">
            <el-form-item label="资源名称" prop="name">
              <el-input v-model="form.name" placeholder="资源名称" clearable></el-input>
            </el-form-item>
            <el-form-item label="路径" prop="url">
              <el-input v-model="form.url" placeholder="路径" clearable></el-input>
            </el-form-item>
            <el-form-item label="资源分类" prop="categoryId">
              <el-select v-model="form.categoryId" placeholder="资源分类" clearable>
                <el-option v-for="item in categoryList" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button @click="onReset">重置</el-button>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmit" :disabled="isLoading">查询</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="main">
          <!-- 主体结构 -->
          <el-table
            :data="tableData"
            style="width: 100%"
            v-loading="isLoading">
            <el-table-column
              type="index"
              label="编号"
              width="100">
            </el-table-column>
            <el-table-column
              prop="name"
              label="资源名称">
            </el-table-column>
            <el-table-column
              prop="url"
              label="资源路径">
            </el-table-column>
            <el-table-column
              prop="description"
              label="描述">
            </el-table-column>
            <el-table-column
              label="添加时间">
              <template slot-scope="scope">
                <span>{{ scope.row.createdTime | dateFormate }}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="操作">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  @click="handleEdit(scope.row)">
                  编辑
                  </el-button>
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.row)">
                  删除
                  </el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页结构 -->
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="form.current"
            :page-sizes="[10, 15, 20]"
            :page-size="form.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount"
            :disabled="isLoading">
          </el-pagination>
        </div>
      </el-card>
    </div>
</template>

<script>
import { loadResourcesPage, getResourceCategories } from '@/services/resources'
export default {
  name: 'ResourceList',
  data () {
    return {
      form: {
        // 当前显示的页数
        current: 1,
        size: 10,
        name: '',
        url: '',
        categoryId: ''
      },
      totalCount: 0,
      // 资源分类列表
      categoryList: [],
      tableData: [],
      isLoading: false
    }
  },
  created () {
    this.loadResources()
    // 加载资源分类列表
    this.loadResourceCategories()
  },
  methods: {
    onReset () {
      this.$refs.form.resetFields()
    },
    async loadResourceCategories () {
      const { data } = await getResourceCategories()
      if (data.code === '000000') {
        this.categoryList = data.data
      }
    },
    // 每页的条数
    handleSizeChange (val) {
      this.form.size = val
      this.form.current = 1
      this.loadResources()
    },
    //  当前显示的页数改变
    handleCurrentChange (val) {
      this.form.current = val
      this.loadResources()
    },
    async loadResources () {
      this.isLoading = true
      const { data } = await loadResourcesPage(this.form)
      if (data.code === '000000') {
        this.tableData = data.data.records
        this.totalCount = data.data.total
        this.isLoading = false
      }
    },
    // 查询按钮事件
    onSubmit () {
      this.form.current = 1
      this.loadResources()
    }
  },
  filters: {
    dateFormate (date) {
      console.log(date)
      date = new Date(date)
      return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}
              ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`
    }
  }
}
</script>

<style lang="scss" scoped ></style>
